from flask import Blueprint, render_template, request, session
from flask_login import login_required, current_user
from sqlalchemy import desc
from applications.common.curd import model_to_dicts, enable_status, disable_status, get_one_by_id
from applications.common import curd
from applications.common.curd import enable_status, disable_status
from applications.common.utils.http import table_api, fail_api, success_api
from applications.common.utils.rights import authorize
from applications.common.utils.validate import str_escape
from applications.extensions import db
from applications.models import Violation


admin_violation = Blueprint('adminViolation', __name__, url_prefix='/admin/violation')


# 用户管理
@admin_violation.get('/')
@authorize("admin:violation:main")
def main():

    return render_template('admin/violation/main.html')


# 表格数据
@admin_violation.get('/data')
@authorize("admin:violation:main")
def table():
    username = str_escape(request.args.get('username', type=str))
    title = str_escape(request.args.get('title', type=str))
    status = str_escape(request.args.get('status', type=str))
    filters = []
    if title:
        filters.append(Violation.title.contains(title))
    if status:
        filters.append(Violation.status.contains(status))
    if username:
        filters.append(Violation.username.contains(username))
    violations = Violation.query.filter(*filters).layui_paginate()
    # return table_api(data=roles.items, count=roles.total)
    return table_api(
        data=[{
            'id': Violation.id,
            'title': Violation.title,
            'commentnum': Violation.commentnum,
            'num': Violation.num,
            'username': Violation.username,
            'enable': Violation.enable,
            'url': Violation.url,
            'create_time': Violation.create_time,
        } for Violation in violations.items],
        count=violations.total)

@admin_violation.delete('/remove/<int:id>')
@authorize("admin:violation:remove", log=True)
def remove(id):
    member = Violation.query.filter_by(id=id).first()
    # 删除该角色的权限和用户
    member.power = []
    member.user = []

    r = Violation.query.filter_by(id=id).delete()
    db.session.commit()
    if not r:
        return fail_api(msg="删除失败")
    return success_api(msg="删除成功")


# 批量删除
@admin_violation.delete('/batchRemove')
@authorize("admin:violation:remove", log=True)
def batch_remove():
    ids = request.form.getlist('ids[]')
    for id in ids:
        member = Violation.query.filter_by(id=id).first()
        # 删除该角色的权限和用户
        member.power = []
        member.user = []

        r = Violation.query.filter_by(id=id).delete()
        db.session.commit()
    return success_api(msg="批量删除成功")

# 启用
@admin_violation.put('/enable')
@authorize("admin:violation:edit", log=True)
def enable():
    id = request.get_json(force=True).get('Id')
    if id:
        res = enable_status(Violation, id)
        if not res:
            return fail_api(msg="出错啦")
        return success_api(msg="操作成功")
    return fail_api(msg="数据错误")


# 禁用
@admin_violation.put('/disable')
@authorize("admin:violation:edit", log=True)
def dis_enable():
    _id = request.get_json(force=True).get('Id')
    if _id:
        res = disable_status(Violation, _id)
        if not res:
            return fail_api(msg="出错啦")
        return success_api(msg="操作成功")
    return fail_api(msg="数据错误")


